package com.viewpager;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import butterknife.ButterKnife;

/**
 * Created by lgs on 2018/3/20.
 * BaseDialogFragment
 */

public abstract class BaseDialogFragment extends DialogFragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);//点击dialog以外的区域(true)关闭、(false)不关闭
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //程序简单起见 直接使用该方法创建一个dialog

        Dialog dialog = new Dialog(getContext(),R.style.ActionSheetDialogStyle);
        dialog.setContentView(R.layout.layout_ok_cancel);

        Button ok =(Button) dialog.findViewById(R.id.btn_ok);
        Button cancel = (Button)dialog.findViewById(R.id.btn_cancel);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != getConfirmDialogListener()) {
                    getConfirmDialogListener().onConfirmDialogPositiveListener();
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != getConfirmDialogListener()) {
                    getConfirmDialogListener().onConfirmDialogCancelListener();
                }
            }
        });

        return dialog;
      /*  AlertDialog.Builder b = new AlertDialog.Builder(getActivity())
                .setTitle("title")
                .setMessage("msg")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                    }
                });
        return b.create();*/

    }

    protected abstract int getLayoutId();

    protected void init() {

    }

    /**
     * 为获取接口类型定义的一个辅助方法 简化每次都要强转的麻烦
     *
     * @param listenerInterface
     * @param <T>
     * @return
     */
    protected <T> T getDialogListener(Class<T> listenerInterface) {
        //用targetFragment是否为空来标识是fragment还是activity开启的这个DialogFragment
        Fragment targetFragment = getTargetFragment();
        if (null != targetFragment && listenerInterface.isAssignableFrom(targetFragment.getClass())) {
            return (T) targetFragment;
        }
        if (null != getActivity() && listenerInterface.isAssignableFrom(getActivity().getClass())) {
            return (T) getActivity();
        }
        return null;
    }

    /**
     * 定义该dialog需要的回调方法
     */
    public interface IConfirmDialogListener {

        void onConfirmDialogPositiveListener();

        void onConfirmDialogCancelListener();
    }

    private IConfirmDialogListener getConfirmDialogListener() {
        return getDialogListener(IConfirmDialogListener.class);
    }
}
